package com.handpay.framework.utils;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

public class DES2PWD {
	public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding"; 
    
    /**
     * 加密
     * @param encryptString
     * @param encryptKey
     * @return
     * @throws Exception
     */
    public static String encryptDES(String encryptString, String encryptKey)
            throws Exception {
        IvParameterSpec zeroIv = new IvParameterSpec(encryptKey.getBytes("utf-8"));
        SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes("utf-8"), "DES");
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
        byte[] encryptedData = cipher.doFinal(encryptString.getBytes());
        return Base64.encodeToString(encryptedData,Base64.NO_WRAP);
    }
    
    /**
     * 解密
     * @param decrypString
     * @param decryptKey
     * @return
     * @throws Exception
     */
    public static String decryptDES(String decrypString, String decryptKey)
            throws Exception {
        byte[] byteMi =Base64.decode(decrypString,Base64.NO_WRAP);
        IvParameterSpec zeroIv = new IvParameterSpec(decryptKey.getBytes("utf-8"));
        SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes("utf-8"), "DES");
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        cipher.init(Cipher.DECRYPT_MODE, key,zeroIv);
        byte[] decryptedData = cipher.doFinal(byteMi);
        return new String(decryptedData);
    }
 
}
